package com.gzist.zzchatroomserver.interceptors;

import cn.dev33.satoken.exception.*;
import cn.dev33.satoken.util.SaResult;
import com.gzist.zzchatroomserver.interceptors.exceptions.LoginFailedException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    // 拦截：未登录异常
    @ExceptionHandler(NotLoginException.class)
    public SaResult handlerException(NotLoginException e) {
        e.printStackTrace();
        log.error("未登录异常：{}", e.getMessage());
        return SaResult.error(e.getMessage()).setCode(401);
    }

    // 拦截：缺少权限异常
    @ExceptionHandler(NotPermissionException.class)
    public SaResult handlerException(NotPermissionException e) {
        e.printStackTrace();
        log.error("缺少权限：{}", e.getPermission());
        return SaResult.error("缺少权限：" + e.getPermission()).setCode(403);
    }

    // 拦截：缺少角色异常
    @ExceptionHandler(NotRoleException.class)
    public SaResult handlerException(NotRoleException e) {
        e.printStackTrace();
        log.error("缺少角色：{}", e.getRole());
        return SaResult.error("缺少角色：" + e.getRole()).setCode(403);
    }

    // 拦截：二级认证校验失败异常
    @ExceptionHandler(NotSafeException.class)
    public SaResult handlerException(NotSafeException e) {
        e.printStackTrace();
        log.error("二级认证校验失败：{}", e.getService());
        return SaResult.error("二级认证校验失败：" + e.getService()).setCode(e.getCode());
    }

    // 拦截：服务封禁异常
    @ExceptionHandler(DisableServiceException.class)
    public SaResult handlerException(DisableServiceException e) {
        e.printStackTrace();
        log.error("当前账号 " + e.getService() + " 服务已被封禁 (level=" + e.getLevel() + ")：" + e.getDisableTime() + "秒后解封");
        return SaResult.error("当前账号 " + e.getService() + " 服务已被封禁 (level=" + e.getLevel() + ")：" + e.getDisableTime() + "秒后解封")
                .setCode(e.getCode());
    }

    // 拦截：Http Basic 校验失败异常
    @ExceptionHandler(NotHttpBasicAuthException.class)
    public SaResult handlerException(NotHttpBasicAuthException e) {
        e.printStackTrace();
        log.error("Http Basic 校验失败：{}", e.getMessage());
        return SaResult.error(e.getMessage()).setCode(e.getCode());
    }

    // 拦截：登录失败异常
    @ExceptionHandler(LoginFailedException.class)
    public SaResult handlerException(LoginFailedException e) {
        e.printStackTrace();
        log.error("登录失败：{}", e.getMessage());
        return SaResult.error("登录失败：" + e.getMessage()).setCode(401);
    }

    // 拦截：其它所有异常
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) // 500 服务器内部错误
    public SaResult handlerException(Exception e) {
        e.printStackTrace();
        log.error("全局未知异常：{}", e.getMessage());
        return SaResult.error(e.getMessage()).setCode(500);
    }
}
